/**
 * 结算面板
 */
class SettleWindow extends how.module.Window {
	public closeButton: how.Button;
	public goon: how.Button;
	public showTotle: how.Button;

	public group_0: eui.Group;
	public banker_0: eui.Image;
	public name_0: eui.Label;
	public point_0: eui.Label;
	public memo_0: eui.Label;
	public hu_0: eui.Image;
	public dianpao_0: eui.Image;
	public score_0: eui.Label;
	public tui_0: eui.Image;
	public liuju_0: eui.Image;
	public group_1: eui.Group;
	public banker_1: eui.Image;
	public name_1: eui.Label;
	public point_1: eui.Label;
	public memo_1: eui.Label;
	public hu_1: eui.Image;
	public dianpao_1: eui.Image;
	public score_1: eui.Label;
	public tui_1: eui.Image;
	public liuju_1: eui.Image;
	public group_2: eui.Group;
	public banker_2: eui.Image;
	public name_2: eui.Label;
	public point_2: eui.Label;
	public memo_2: eui.Label;
	public hu_2: eui.Image;
	public dianpao_2: eui.Image;
	public score_2: eui.Label;
	public tui_2: eui.Image;
	public liuju_2: eui.Image;
	public group_3: eui.Group;
	public banker_3: eui.Image;
	public name_3: eui.Label;
	public point_3: eui.Label;
	public memo_3: eui.Label;
	public hu_3: eui.Image;
	public dianpao_3: eui.Image;
	public score_3: eui.Label;
	public tui_3: eui.Image;
	public liuju_3: eui.Image;

	private data: any;
	public constructor() {
		super();
		this.skinName = "SettleWindowSkin";
	}

	public start(): void {
		this.closeButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.close, this);
		this.goon.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGoon, this);
		this.showTotle.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onShowTotle, this);
		//this.closeWhenTouchBg = false;
		how.SoundManager.playEffect("audio_win_mp3");
		var scene = (D.App.gameView as MJGameSceneView);
		//scene.resetGameScene();
	}

	public onGameStart(e: egret.Event): void {
		this.close();
	}

	public onGoon(): void {
		this.close();
		var scene = (D.App.gameView as MJGameSceneView);
		scene.ready();
	}

	public theGameEnd(): void {
		this.goon.visible = false;
		this.showTotle.visible = true;
	}
	public onShowTotle(): void {
		console.log(">>> SettleWindow onShowTotle 显示结算信息");
		//var win = how.Application.openWindow(TotleSettleWindow) as TotleSettleWindow;
		//win.setData(this.data);
	}
	/**
	 * 9,107 数据：{"cbCardCount":[13,10,10,10],
	 * "cbCardData":[[18,19,22,23,24,24,24,35,35,37,37,40,40,0],[17,18,19,20,20,20,23,24,38,39,0,0,0,0],[21,21,22,22,23,23,35,37,38,40,0,0,0,0],[2,3,4,4,5,6,19,19,20,21,0,0,0,0]],
	 * "wProvideUser":[0,1,2,3],
	 * "dwChiHuRight":[0,0,0,0],
	 * "lGameScore":[-1,-1,-1,3],
	 * "lGameTax":[0,0,0,0],
	 * "wWinOrder":[0,0,0,0],
	 * "wGangScore":[0,0,0,0],
	 * "wLostFanShu":[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],
	 * "bLiuju":1,
	 * "cbAGNum":[0,0,0,0],"cbPGNum":[0,0,0,1]
	 * "cbLeftCards":[33,7,18,34,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}
	 */
	public setData(d: any): void {
		this.data = d;
		let gs:MJGameSceneView = <MJGameSceneView>D.App.gameView;
		//庄家
		(this["banker_" + gs.c2p(gs.banker)] as eui.Image).visible = true;
		//遍历玩家信息
		for (let j = 0; j < this.data.cbCardData.length; j++) {
			var cID = gs.p2c(j);//当前座位玩家的位置
			console.log(">>> SettleWindow,位置号",j," 椅子号：",cID,);
			//玩家手牌展示-暂时只展示手牌
			var usercars:Array<number> =this.data.cbCardData[cID];
			usercars.length = this.data.cbCardCount[cID];
			usercars.sort(function (a, b) { return a >= b ? 1 : -1 });
			var groupIndex = 0;
			var nowGroup = (this["group_" + j] as eui.Group).getChildByName(groupIndex.toString()) as eui.Group;
			groupIndex++;
			for (let n = 0; n < usercars.length; n++) {
				var card = new SettleCard();
				card.setData(usercars[n]);
				nowGroup.addChild(card);
			}
			//是否胡牌及供牌玩家
			if(gs.huPaiData)
			{
				if(gs.huPaiData.wChiHuUser == cID)//当前玩家胡牌
				{
					this["memo_"+j].text = MjLogic.getCardType(this.data.dwChiHuRight[cID]);
					this["hu_" + j].visible = true;
					this["dianpao_" + j].visible = false;
					//展示胡牌
					var nowGroup = (this["group_" + j] as eui.Group).getChildByName(groupIndex.toString()) as eui.Group;
					groupIndex++;
					var card = new SettleCard();
					card.setData(gs.huPaiData.cbChiHuCard);
					nowGroup.addChild(card);
				}else if(gs.huPaiData.wProviderUser == cID)//点炮玩家
				{
					this["memo_"+j].text = "";
					this["hu_" + j].visible = false;
					this["dianpao_" + j].visible = true;
				}
			}else
			{
				this["hu_" + j].visible = false;
				this["dianpao_" + j].visible = false;
			}
			//玩家基本信息展示
			(this["name_" + j] as eui.Label).text = (<MJPlayerView>gs.playerList[j]).nickNameLabel.text;
			if (this.data.bLiuju == 0) {
				(this["score_" + j] as eui.Label).text = this.data.lGameScore[cID].toString();
				if (this.data.lGameScore[cID] >= 0) {
					(this["score_" + j] as eui.Label).textColor = 0x04D035;
				} else {
					(this["score_" + j] as eui.Label).textColor = 0xF8322D;
				}
			} else {
				(this["score_" + j] as eui.Label).text = this.data.lGameScore[cID].toString();
				(this["score_" + j] as eui.Label).textColor = 0x04D035;
				(this["liuju_" + j] as eui.Image).visible = true;
			}
			//this["tui_0"].visible = true;//退出
			//this["point_0].text = "加番?";
		}
	}
}